# -*- coding: utf-8 -*-
# @Time    : 2024/9/25 13:53
# @Author  : Alvin
# @File    : ApiGetUserInfo.py
# @Des     : 用户信息
import json
import logging
import traceback

from tornado.concurrent import run_on_executor
from tornado.gen import coroutine

from common.http_serv import app
from server.base_request import BaseHttpHandler
from server.handler.UserInfoHandler import GetUserInfoHandler


@app.route("/addUser")
class ApiAddUser(BaseHttpHandler):
    """
        增加用户余额
    """

    @coroutine
    def post(self):
        """
        post消息处理
        :return:
        """
        data = yield self.post_async()
        self.write(data)
        self.finish()

    @run_on_executor
    def post_async(self):
        """
        post_async消息处理
        :return:
        """
        try:
            rec_data = json.loads(self.request.body.decode())
            hanlder = GetUserInfoHandler()
            result = hanlder.handle_add_user(rec_data)
            logging.info(f"增加余额参数：{rec_data},响应：{result}")
            return {"rsp": result}
        except Exception as ex:
            logging.error("getLogisticsNum ex:%s", ex)
            logging.error("traceback.format_exc():\n%s" % traceback.format_exc())
            return str(ex)


@app.route("/getUserInfo")
class ApiGetUserInfo(BaseHttpHandler):
    """
    获取用户信息
    """

    @coroutine
    def get(self):
        data = yield self.get_async()
        self.write(data)
        self.finish()

    @run_on_executor
    def get_async(self):
        """
        get消息处理
        :return:
        """
        try:
            arguments = self.request.arguments
            logging.info(f"获取用户信息参数：{arguments}")
            userId = self.get_query_argument("userId")
            hanlder = GetUserInfoHandler()
            rsp = hanlder.get_user_info_by_id(userId)
            return {"rsp": rsp}
        except Exception as ex:
            logging.error("getUserInfo ex:%s", ex)
            logging.error("traceback.format_exc():\n%s" % traceback.format_exc())
            return str(ex)


@app.route("/addUserBalance56263")
class ApiAddUserBalance(BaseHttpHandler):
    """
        增加用户余额
    """

    @coroutine
    def post(self):
        """
        post消息处理
        :return:
        """
        data = yield self.post_async()
        self.write(data)
        self.finish()

    @run_on_executor
    def post_async(self):
        """
        post_async消息处理
        :return:
        """
        try:
            rec_data = json.loads(self.request.body.decode())
            hanlder = GetUserInfoHandler()
            result = hanlder.add_user_balance(rec_data)
            logging.info(f"增加余额参数：{rec_data},响应：{result}")
            return {"rsp": result}
        except Exception as ex:
            logging.error("getLogisticsNum ex:%s", ex)
            logging.error("traceback.format_exc():\n%s" % traceback.format_exc())
            return str(ex)
